﻿namespace EFS.RC.CommunicatorContext.Operators
{
	public partial class CoversationClassification : EFS.RC.CommunicatorContext.Common.Pages.Controls.Control
	{
		#region Delegates
		/// SelectionChanged
		public delegate void SelectionChangedDelegate(System.Int32 newSelection);
		public SelectionChangedDelegate SelectionChanged;
		#endregion

		public CoversationClassification()
		{
			InitializeComponent();

			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				UtilitiesService.Classifications_GetByLineIdCompleted += new System.EventHandler<CommunicatorContext.ServiceReference.Classifications_GetByLineIdCompletedEventArgs>(UtilitiesService_Classifications_GetByLineIdCompleted);
			}
		}

		#region Properties

		/// Property SelectedType: Get
		public System.Int32 SelectedType
		{
			get
			{
				// test
				if (null != comboBoxClassification.SelectedValue)
				{
					// ID
					return (System.Int32)comboBoxClassification.SelectedValue;
				}

				// none
				return 0;
			}
		}

		#endregion

		#region Usage

		/// <summary>
		/// Load 
		/// </summary>
		/// <param name="lineId"></param>
		public void LoadClassifications(System.Int32 lineId)
		{
			UtilitiesService.Classifications_GetByLineIdAsync(lineId);
		}

		#endregion

		#region Events
		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// ignore in designer
			if (System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				return;
			}
		}

		/// Selection changed
		private void comboBoxClassification_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
		{
			// notify all registered
			if (null != this.SelectionChanged)
			{
				this.SelectionChanged(SelectedType);
			}
		}

		void UtilitiesService_Classifications_GetByLineIdCompleted(object sender, ServiceReference.Classifications_GetByLineIdCompletedEventArgs e)
		{
			// check
			if (null != e.Error)
			{
				// show error
				ShowErrorMessage("Failed to retrieve classifications.", e.Error);

				// done
				return;
			}

			// check
			if (null == e.Result)
			{
				// done
				return;
			}

			try
			{
				// get array
				EFS.RC.CC.Messages.Communicator.Lync.Context.Classification[] array =
						EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.Messages.Communicator.Lync.Context.Classification[]>(e.Result.ToString());

				// set source
				comboBoxClassification.ItemsSource = array;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Unexpected error occurred when retrieving classifications.", ex);
			}
		}
		#endregion
	}

}